using System;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// <see cref="IQueue{T}"/>
	/// </summary>
	public class Queue<T> : IQueue<T> where T : class
	{
		private ICloudQueue dataToValidateQueue;

		/// <summary>
		/// Ctor
		/// </summary>
		public Queue(ICloudStorageAccountManager cloudStorageAccountManager, string context, string queueName)
		{
			dataToValidateQueue = cloudStorageAccountManager.GetQueueReference(context, queueName);
		}

		/// <summary>
		/// <see cref="IQueue{T}.Enqueue"/>
		/// </summary>
		public void Enqueue(CloudQueueMessage<T> queueMessage)
		{
			dataToValidateQueue.AddMessage(queueMessage);
		}

		/// <summary>
		/// <see cref="IQueue{T}.Dequeue"/>
		/// </summary>
		public CloudQueueMessage<T> Dequeue(TimeSpan visibilityTimeout)
		{
			return dataToValidateQueue.GetMessage<T>(visibilityTimeout);
		}

		/// <summary>
		/// <see cref="IQueue{T}.Delete"/>
		/// </summary>
		public void Delete(CloudQueueMessage<T> toDelete)
		{
			dataToValidateQueue.DeleteMessage(toDelete);
		}

		/// <summary>
		/// <see cref="IQueue{T}.HasMessages"/>
		/// </summary>
		public bool HasMessages()
		{
			return dataToValidateQueue.PeekMessage() != null;
		}
	}
}